//
//  LS_OAuthViewController.swift
//  weibo
//
//  Created by 罗李苏 on 16/5/23.
//  Copyright © 2016年 Apple. All rights reserved.
//

import UIKit
import SVProgressHUD
class LS_OAuthViewController: UIViewController,UIWebViewDelegate {
    
    //声明webView属性
    var webView = UIWebView()
    
    override func loadView() {
        //直接把webView作为根视图
        view = webView

        webView.delegate = self
        
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        setNavBar()
        loadOauthPage()

        }
    
    //打开授权界面
    private func loadOauthPage(){
        let urlString = "https://api.weibo.com/oauth2/authorize?" + "client_id=" + client_id + "&redirect_uri=" + redirect_uri
        
        let url = NSURL(string: urlString)
        
        let request = NSURLRequest(URL: url!)
        
        webView.loadRequest(request)
        
    }
}
    //MARK:设置导航条 监听按钮点击事件
    extension LS_OAuthViewController{
    private func setNavBar(){
        //关闭按钮
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "关闭", style: .Plain, target: self, action: #selector(LS_OAuthViewController.close))
        
        //测试按钮
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "GM通道", style: .Plain, target: self, action: #selector(LS_OAuthViewController.testAccount))
    }
    
    //关闭按钮的点击事件
    @objc private func close(){
        
        dismissViewControllerAnimated(true, completion: nil)
    
    }
    
    //测试按钮的点击事件
    @objc private func testAccount(){
        
        //使用webView 执行js 字符串
        //js 动态注入
        let jsString = "document.getElementById('userId').value = '18630149091', document.getElementById('passwd').value = 'lisuaizjl228' "
        webView.stringByEvaluatingJavaScriptFromString(jsString)
    
    }
    
    }
    
   //MARK:使用进度条显示下载进度
extension LS_OAuthViewController{
    //开始下载的时候显示下载指示器
    func webViewDidStartLoad(webView: UIWebView) -> () {
        
        //显示用户等待指示器
        SVProgressHUD.show()
    }
    
    //下载结束后隐藏下载指示器
    func webViewDidFinishedLoad(webView: UIWebView) -> () {
        
        SVProgressHUD.dismiss()
    }
    
    
    //授权成功之后 就会回调新页面  在回调页面的地址中会包含code(授权码)
    
    func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool{
        
        //打印URL
        let urlString = request.URL?.absoluteString ?? ""
        
        //截取授权成功的字符串中的code
        //定义授权成功的标记
        let successFlag = "code="
        if urlString.containsString(successFlag) {
            //授权成功
            let rang = urlString.rangeOfString(successFlag)!
            let code = urlString.substringFromIndex(rang.endIndex)
            print(code,urlString)
            //获取code 之后 应该使用 code码 请求token(用户访问令牌)
            LS_UserAccountViewModel.sharedAccountViewModel.loadAccessToken(code, finished: { (isSuccess) in
                if !isSuccess{
                    SVProgressHUD.showErrorWithStatus(AppErrorTip)
                    return
                }
                //请求成功发送通知
                self.dismissViewControllerAnimated(false, completion: { 
                    
                    NSNotificationCenter.defaultCenter().postNotificationName(SwitchRootViewController, object: nil)
                })
            })
             //不加载网页
            return false
        }
              return true
    
    }

}
    
    
    

